<?php

/* @(#) $Id: nulldb.php 3 2009-02-16 17:58:10Z mo@ilaven.net $ */

class EFNullDBEngine extends EFDatabaseEngine
{
	protected $connectionClass = 'EFNullDBConnection';
}

class EFNullDB extends EFSQLDatabase
{
	public function prepare($statement, $offset = 0, $limit = 0)
	{
		$statement = null;
		return $statement;
	}
	
	public function insertId()
	{
		return null;
	}
	
	public function beginTransaction()
	{
		return false;
	}
	
	public function rollbackTransaction()
	{
		return false;
	}
	
	public function commitTransaction()
	{
		return false;
	}
}

class EFNullDBConnection extends EFDatabaseConnection
{
	protected $registryClass = 'EFNullDBRegistry';
	protected $databaseClass = 'EFNullDB';
	
	public function &prepare($sql, $offset = 0, $limit = 0)
	{
		raise("Attempt to prepare SQL statement when using Null database driver: " . $sql);
	}
	
	public function getTable($db, $name, $createMode)
	{
		return null;
	}
	
	public function beginTransaction()
	{
		return false;
	}

	public function commitTransaction()
	{
		return false;
	}
	
	public function rollbackTransaction()
	{
		return false;
	}
}

class EFNullDBRegistry extends EFRegistry
{
	protected $realms = array(
		'(Top)' => array(
			'id' => 1,
			'name' => '(Top)',
			'parentId' => null,
			'title' => null,
			'distinguishedName' => null,
			'identifier' => null,
			'style' => null,
			'domain' => null,
			),
		);
	protected $instances = array();
	
	public function getGlobalModules()
	{
		return array(
			'com.googlecode.eregansu.foundation.nulldb' => 0,
			);
	}
	
	public function getRealmModules($realmId)
	{
		return array();
	}
	
	public function schemaLock()
	{
		return false;
	}
	
	public function schemaUnlock()
	{
		return false;
	}
	
	public function schemaLog($msg)
	{
		dprint($msg);
	}
	
	public function updateModuleVersion($identifier, $version)
	{
		dprint('Updated '. $identifier . ' to ' . $version);
	}
	
	public function getRealms($autocreateRealm = null)
	{
		if(!isset($this->realms[$autocreateRealm]))
		{
			$this->realms[$autocreateRealm] = array(
				'id' => count($this->realms) + 1,
				'name' => $autocreateRealm,
				'parentId' => 1,
				'title' => null,
				'dn' => null,
				'identifier' => null,
				'style' => null,
				'dnsDomain' => null,
				);
		}
		return $this->realms;
	}
	
	public function getRealmInfo($nameOrId, $autoCreate = false, $useCache = false)
	{
		if(is_numeric($nameOrId))
		{
			foreach($this->realms as $r)
			{
				if($r['id'] == $nameOrId) return $r['id'];
			}
			return null;
		}
		if(isset($this->realms[$nameOrId]))
		{
			return $this->realms[$nameOrId];
		}
		if($autoCreate)
		{
			$this->realms[$nameOrId] = array(
				'id' => count($this->realms) + 1,
				'name' => $nameOrId,
				'parentId' => 1,
				'title' => null,
				'dn' => null,
				'identifier' => null,
				'style' => null,
				'dnsDomain' => null,
				);
			return $this->realms[$nameOrId];
		}
		return null;
	}
	
	public function getInstances($autocreateInstance = null)
	{
		if(!isset($this->instances[$autocreateInstance]))
		{
			$this->instances[$autocreateInstance] = array(
				'id' => count($this->instances),
				'name' => $autocreateInstance,
				'description' => null,
				'clusterId' => 1,
				);
		}
		return $this->instances;
	}
	
	public function getInstance($nameOrId, $autocreate = false)
	{
		if(!$autocreate || is_numeric($nameOrId))
		{
			$autocreateInstance = null;
		}
		else
		{
			$autocreateInstance = $nameOrId;
		}
		$list = $this->getInstances($autocreateInstance);
		if(is_numeric($nameOrId))
		{
			foreach($this->instances as $i)
			{
				if($i['id'] == $nameOrId) return $i;
			}
		}
		else
		{
			if(isset($list[$nameOrId]))
			{
				return $list[$nameOrId];
			}
		}
		return null;
	}
	
	public function getCluster($clusterId)
	{
		if($clusterId == 1)
		{
			return array('cluster_id' => 1, 'cluster_name' => 'default', 'cluster_description' => null);
		}
		return null;
	}
	
	public function getClusterLinks($clusterId)
	{
		return array();
	}
}

export_for('com.googlecode.eregansu.foundation.nulldb', 'EFNullDBEngine');
